package com.vf.admin.common;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vf.admin.client.service.AppHeadwearService;
import com.vf.admin.client.service.AppPrivilegeService;
import com.vf.admin.common.filehandle.YSensitiveUtil;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppHeadwear;
import com.vf.admin.miaodong.entity.AppPrivilege;
import com.vf.admin.miaodong.entity.AppSensitive;
import com.vf.admin.miaodong.service.AfterSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.*;


/**
 * 应用启动时初始化数据
 */
@Component
public class ApplicationInitData implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    AppPrivilegeService appPrivilegeService;
    @Autowired
    AppHeadwearService appHeadwearService;
    @Autowired
    AfterSensitiveService afterSensitiveService;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            if (!getLocalPort().equals(Constants.CHUSHIHUA_DUANKOU)) {
            }
            //初始化数据快
            List<AppPrivilege> guizuList = appPrivilegeService.guizuList();
            for (AppPrivilege appPrivilege : guizuList) {
                Constants.guizuCacheMap.put(appPrivilege.getId(), appPrivilege.getMedal());
                Constants.guizuMingMap.put(appPrivilege.getId(), appPrivilege.getName());
            }
            List<AppHeadwear> toushiList = appHeadwearService.findList();
            for (AppHeadwear headwear : toushiList) {
                Constants.toushiCacheMap.put(headwear.getId(), headwear.getImgurl());
            }
            Map<String, Object> map = new HashMap<>(16);
            map.put("state", 1);
            List<AppSensitive> minganci = afterSensitiveService.findList(map, null);
            if (minganci.size() > 0) {
                for (AppSensitive sensitive : minganci) {
                    Constants.keyWordSet.add(sensitive.getSensi());
                }
            }
            //铭感词从阿里云oss获取
            String stringJson = YSensitiveUtil.readJsonFile();
            //
            if (ObjectUtil.isNotEmpty(stringJson)) {
                JSONObject jsonObject = JSON.parseObject(stringJson);
                JSONArray jsonArray = jsonObject.getJSONArray("content");
                Constants.jsonArray.addAll(jsonArray);
            }

            Constants.heartSayHelloCacheNv.add("听说主动就会有故事，是这样吗？");
            Constants.heartSayHelloCacheNv.add("我看人很准的，一看你就是有缘人");
            Constants.heartSayHelloCacheNv.add("嗨，你也是来找对象吗？");
            Constants.heartSayHelloCacheNv.add("希望我是一个让你心动的人~");
            Constants.heartSayHelloCacheNv.add("嗨~你也是来找真爱的？");
            Constants.heartSayHelloCacheNv.add("你说你吧，长得好看也就算了，还长成我喜欢的样子，你让我怎么办?");
            Constants.heartSayHelloCacheNv.add("听说给帅哥发信息都会回我的~");
            Constants.heartSayHelloCacheNv.add("叮咚，你的小可爱已上线");
            Constants.heartSayHelloCacheNv.add("很高兴在这里遇到你，交个朋友吗？");
            Constants.heartSayHelloCacheNv.add("嗨，你也是单身吗？");

            Constants.heartSayHelloCacheNan.add("听说给美女发信息都会回我的~");
            Constants.heartSayHelloCacheNan.add("嗨，你相信缘分吗？");
            Constants.heartSayHelloCacheNan.add("听说你是最可爱的女孩子");
            Constants.heartSayHelloCacheNan.add("不管结果如何，可以认识一下吗？");
            Constants.heartSayHelloCacheNan.add("哪里来的绝世大宝贝！");
            Constants.heartSayHelloCacheNan.add("你好呀，可以认识一下吗？");
            Constants.heartSayHelloCacheNan.add("能给个相互了解的机会吗？");
            Constants.heartSayHelloCacheNan.add("你是吃可爱长大的吗？");
            Constants.heartSayHelloCacheNan.add("你一定是某人最喜欢的惊喜。");
            Constants.heartSayHelloCacheNan.add("相逢是缘，很高兴认识你~");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @return
     * @throws MalformedObjectNameException 获取当前tomcat机器的端口号
     */
    public String getLocalPort() throws MalformedObjectNameException {
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        String port = objectNames.iterator().next().getKeyProperty("port");
        return port;
    }

}

